package com.flx.ark.algo.limit;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author Fenglixiong
 * @Create 2021/12/14 0:27
 * @Description 计数器限流算法
 **/
public class CountLimit {

    private volatile long startTime;//开始时间

    private static final long MIN = 60*1000;//1分钟

    private static final int LIMIT = 100;//1分钟限制100个请求

    private AtomicInteger count = new AtomicInteger(0);

    private boolean apiPass(){
        long currentTime = System.currentTimeMillis();
        if(currentTime-startTime >= MIN){
            startTime = currentTime;
            count.set(0);
        }
        if(count.get()>LIMIT){
            return false;
        }
        //doSom
        count.addAndGet(1);
        return true;
    }

    public static void main(String[] args) {



    }

}
